import { Component } from '@angular/core';
import { NavController, NavParams,ModalController } from 'ionic-angular';
import { MyCommentPage } from '../my-comment/my-comment';
import { AllCommentsPage } from '../all-comments/all-comments';
import { DataService } from '../../services/data';
import Avatars from '@dicebear/avatars';
import SpriteCollection from '@dicebear/avatars-identicon-sprites';
import {DomSanitizer} from '@angular/platform-browser'
import { GameRuntimePage } from '../game-runtime/game-runtime';
import { AppGlobal } from '../../app/app.global';
import { LoginPage } from '../login/login';


/**
 * Generated class for the GamePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-game',
  templateUrl: 'game.html',
  providers: [DataService]
})
export class GamePage {

  avatars = new Avatars(SpriteCollection);

  public game:any;
  public comments:any;
  descHeight:string = "75px";
  btnTextShowDesc = "查看完整描述"
  constructor(public navCtrl: NavController, public modalCtrl: ModalController,public navParams: NavParams, public dataService: DataService, private sanitizer: DomSanitizer) {
    let game = navParams.get('game')
    game.desc = this.sanitizer.bypassSecurityTrustHtml(game.desc)
    this.game = game;
    console.log(this.game)
  }

  ionViewDidLoad() {
    this.dataService.getComments(this.game.id, 2).subscribe((data) => {
      for(let item of data){
        item.icon = this.sanitizer.bypassSecurityTrustHtml(this.getIcon(item.user_id))
      }
      this.comments = data;
    })
  }

  getIcon(user_id){
    let svg = this.avatars.create(user_id);
    return svg;
  }

  goBack(){
    this.navCtrl.pop();
  }
  showDescModel(){
    if(this.btnTextShowDesc == "查看完整描述"){
      this.descHeight = "100%";
      this.btnTextShowDesc = "折叠描述";
    }else{
      this.descHeight = "75px";
      this.btnTextShowDesc="查看完整描述";
    }
  }
  showMyComment(){
    
    // let modal = this.modalCtrl.create(MyCommentPage, {game_id : this.game.id});
    // modal.present();

    if(AppGlobal.getInstance().isLogin){
      this.navCtrl.push(MyCommentPage, {game_id : this.game.id});
    }else{
      let modal = this.modalCtrl.create(LoginPage, {canBack: true});
      modal.present();
    }

  }
  showAllComments(){
    this.navCtrl.push(AllCommentsPage, {game_id : this.game.id});
  }

  runGame(){
    if(AppGlobal.getInstance().isLogin){
      this.navCtrl.push(GameRuntimePage, {game : this.game});
      event.stopPropagation();
    }else{
      let modal = this.modalCtrl.create(LoginPage, {canBack: true});
      modal.present();
    }
  }
}
